package com.fenghongzhang.kuaishou.widget;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.fenghongzhang.kuaishou.R;

import java.util.Random;

public class BezierView extends RelativeLayout {

    //随机出现的图片的数组
    private Drawable[] drawables = new Drawable[5];

    //当前view的宽
    int width;
    //当前view的高
    int height;

    public BezierView(Context context) {
        super(context);
    }

    public BezierView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    //初始化数据
    private void init(Context context,AttributeSet attrs) {
        //初始化数组
        drawables[0] = context.getDrawable(R.mipmap.ic_launcher);
        drawables[1] = context.getDrawable(R.mipmap.ic_launcher);
        drawables[2] = context.getDrawable(R.mipmap.ic_launcher);
        drawables[3] = context.getDrawable(R.mipmap.ic_launcher);
        drawables[4] = context.getDrawable(R.mipmap.ic_launcher);
    }

    /**
     * 外部调用这个方法,添加一个图片,并且通过动画让view移动起来.
     */
    public void addImageView(){
        // 0 - 4
        ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[new Random().nextInt(drawables.length)]);

        //把创建的image添加到这个布局中
        addView(imageView);

        //开始一个动画.
        getAnimator(imageView).start();

    }

    //删除所有的view .如果不删除,这个view中,会有很多ImageView.
    public void removeImageView(){
        removeAllViews();
    }

    /**
     * 创建一个动画对象.
     * @param view
     * @return
     */
    public ValueAnimator getAnimator(final View view){
        MyEvaluation myEvaluation = new MyEvaluation(getFointF(), getFointF());
        /**
         * 1.估值器. 自定义估值器,算出不同的坐标点.
         * 2,起始点. 从宽度的中间位置开始,高就是底部的位置
         * 3,结束点. 他的范围就是宽度从0 到本身的宽, 高度是100范围以内的.
         */
        PointF endPointF = new PointF((float) (Math.random() * width), (float) (Math.random() * 100));
        PointF startPointF = new PointF(width/2,height);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaluation, startPointF, endPointF);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                view.setX(pointF.x);
                view.setY(pointF.y);
                //透明度的变化.
//                view.setAlpha(1 - animation.getAnimatedFraction());
            }
        });
        valueAnimator.setDuration(3000);
        valueAnimator.setTarget(view);
        return valueAnimator;
    }

    //Math.random() 0-1
    //随机得到坐标点
    public PointF getFointF(){
        PointF pointF = new PointF();
        pointF.x = (float) (Math.random()*width);
        pointF.x = (float) (Math.random()*height/4);
        return pointF;
    }

    //三次贝塞尔曲线
    public class MyEvaluation implements TypeEvaluator<PointF> {

        private PointF pointF1;
        private PointF pointF2;

        public MyEvaluation(PointF pointF1, PointF pointF2) {
            this.pointF1 = pointF1;
            this.pointF2 = pointF2;
        }

        //计算的方法
        @Override
        public PointF evaluate(float time, PointF startValue, PointF endValue) {
            //三次bezier
            float timeLeft = 1.0f - time;
            PointF point = new PointF();//结果

            //坐标x的计算和坐标y的计算是通过三次bezier的公式得来的.
            point.x = timeLeft * timeLeft * timeLeft * (startValue.x)
                    + 3 * timeLeft * timeLeft * time * (pointF1.x)
                    + 3 * timeLeft * time * time * (pointF2.x)
                    + time * time * time * (endValue.x);

            point.y = timeLeft * timeLeft * timeLeft * (startValue.y)
                    + 3 * timeLeft * timeLeft * time * (pointF1.y)
                    + 3 * timeLeft * time * time * (pointF2.y)
                    + time * time * time * (endValue.y);
            return point;
        }
    }


    /**
     * 测量出宽和高
     * 为view运动轨迹设置范围
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

}
